package cn.iocoder.yudao.module.shop.service.giftcardrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.giftcardrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftcardrecord.ShopGiftCardRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.giftcardrecord.ShopGiftCardRecordConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.giftcardrecord.ShopGiftCardRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 礼品卡绑定记录 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopGiftCardRecordServiceImpl implements ShopGiftCardRecordService {

    @Resource
    private ShopGiftCardRecordMapper giftCardRecordMapper;

    @Override
    public Long createGiftCardRecord(ShopGiftCardRecordCreateReqVO createReqVO) {
        // 插入
        ShopGiftCardRecordDO giftCardRecord = ShopGiftCardRecordConvert.INSTANCE.convert(createReqVO);
        giftCardRecordMapper.insert(giftCardRecord);
        // 返回
        return giftCardRecord.getId();
    }

    @Override
    public void updateGiftCardRecord(ShopGiftCardRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateGiftCardRecordExists(updateReqVO.getId());
        // 更新
        ShopGiftCardRecordDO updateObj = ShopGiftCardRecordConvert.INSTANCE.convert(updateReqVO);
        giftCardRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteGiftCardRecord(Long id) {
        // 校验存在
        validateGiftCardRecordExists(id);
        // 删除
        giftCardRecordMapper.deleteById(id);
    }

    private void validateGiftCardRecordExists(Long id) {
        if (giftCardRecordMapper.selectById(id) == null) {
            throw exception(GIFT_CARD_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ShopGiftCardRecordDO getGiftCardRecord(Long id) {
        return giftCardRecordMapper.selectById(id);
    }

    @Override
    public List<ShopGiftCardRecordDO> getGiftCardRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return giftCardRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopGiftCardRecordDO> getGiftCardRecordPage(ShopGiftCardRecordPageReqVO pageReqVO) {
        return giftCardRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopGiftCardRecordDO> getGiftCardRecordList(ShopGiftCardRecordExportReqVO exportReqVO) {
        return giftCardRecordMapper.selectList(exportReqVO);
    }

}
